<?php
// start session
// session_start();
// set timeout period in seconds
// $inactive = 600;

class logmein{
	// encryption
	var $encrypt = false; // set to true to use md5 encryption for the password

	function __construct(){
		logmein::init();
	}

	function init(){
		if (!isset($_SESSION)){
// 			$endtime = strtotime('tomorrow') - time();
// 			$endtime = round($endtime/60); // minutes

			// set the cache limiter to 'private'
			session_cache_limiter('private');
			session_cache_expire(10); // 10 minutes
			session_start();
		}
	}

	// login function
	function login($username, $password) {
		global $db;

		// check if encryption is used
		if ($this->encrypt == true)
			$password = md5($password);

		// execute login via qry function that prevents MySQL injections
		$Qry = 'SELECT u_pass
				FROM users
				WHERE u_active = 1 AND u_login = "' . $username . '" AND u_pass = "' . $password . '"';

		if ($db->numRows($Qry) > 0){
			$res = $db->fetchOne($Qry);
			// register sessions, you can add additional sessions here if needed
			$_SESSION['loggedin'] = md5($res);

			return true;
		}else{
			session_destroy();
			return false;
		}
	}

	// logout function
	function logout() {
		session_destroy();

		header("HTTP/1.1 301 Moved Permanently");
		header('Location: /admin/login.html');
		exit();
	}

	// Check if logedin
	function checklogin() {
		if (!isset($_SESSION['loggedin'])){

			header("HTTP/1.1 301 Moved Permanently");
			header('Location: /admin/login.html');
			exit();
		}
	}
}